package main

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()

	r.GET("/user/:id", func(c *gin.Context) {
		s := &struct {
			Id   int    `form:"id" json:"id" binding:"required"`
			Name string `form:"name" json:"name" binding:"required"`
		}{
			Id:   1,
			Name: "test",
		}

		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": s,
		})
	})

	r.POST("/user", func(c *gin.Context) {
		s := &struct {
			Id   int    `form:"id" json:"id" binding:"required"`
			Name string `form:"name" json:"name" binding:"required"`
		}{}
		data, err := c.GetRawData()
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code":    400,
				"message": fmt.Sprintf("Error parsing form: %s", err.Error()),
			})
			return
		}
		json.Unmarshal(data, s)
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": s,
		})
	})

	r.Run(":8080")
}
